package boot.spring.controller;

import boot.spring.pagemodel.DataGrid;
import boot.spring.po.Picture;
import boot.spring.service.PictureService;
import boot.spring.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping(value = "/mongodb")
public class MongodbController {

    @Autowired
    private TestService testService;

    @Resource(name = "mongodbServiceImpl")
    private PictureService mongodbService;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public Object test() {
//        testService.createMongoGeoUsers();
        testService.nearQuery();
        testService.nearSphere();
        return null;
    }

    @RequestMapping(value = "/pictures", method = RequestMethod.GET)
    @ResponseBody
    public DataGrid<Picture> getpiclist(@RequestParam("current") int current, @RequestParam("rowCount") int rowCount
            , @RequestParam(required = false, value = "sort[id]") String sortid
            , @RequestParam(required = false, value = "searchPhrase") String searchPhrase) {
        if (searchPhrase == null) {
            List<Picture> list = mongodbService.getpiclist(current, rowCount, sortid);
            int total = mongodbService.getpicturenum();
            DataGrid<Picture> grid = new DataGrid<Picture>();
            grid.setCurrent(current);
            grid.setRowCount(rowCount);
            grid.setRows(list);
            grid.setTotal(total);
            return grid;
        } else {
            List<Picture> list = mongodbService.getsearchresult(current, rowCount, sortid, searchPhrase);
            int total = mongodbService.getsearchresulttotal(searchPhrase);
            DataGrid<Picture> grid = new DataGrid<Picture>();
            grid.setCurrent(current);
            grid.setRowCount(rowCount);
            grid.setRows(list);
            grid.setTotal(total);
            return grid;
        }
    }

    @RequestMapping(value = "/index")
    public String showpic() {
        return "mongodb";
    }

    @RequestMapping(value = "picture/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public void deletepicture(@PathVariable("id") String id) {
        mongodbService.deletePicture(id);
    }

    @RequestMapping(value = "/picture/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Picture getpicture(@PathVariable("id") String id) {
        Picture p = mongodbService.getPictureByid(id);
        return p;
    }

    //这个方法被新增和修改请求共用,如果是新增，id必须为空，否则是修改请求
    @RequestMapping(value = "/picture", method = RequestMethod.POST)
    public String addpicture(@ModelAttribute("picture") Picture picture) {
        mongodbService.SaveorUpdatePicture(picture);
        return "mongodb";
    }

    //创造的测试数据
    @RequestMapping(value = "add", method = RequestMethod.GET)
    @ResponseBody
    public void ddd() {
        for (int i = 0; i < 10000; i++) {
            Picture p = new Picture("w", "java", (long) i);
            mongodbService.SaveorUpdatePicture(p);
        }

    }

}
